<?php

/**
 * 获取商品
 * @param  boolean true 默认缓存 ， false 数据库
 * @return array 商品数组
 */
function getCouponList($cache = true)
{
    $cacheKey = C('CACHE_KEY_COUPON');
    $coupon = S($cacheKey);
    if (empty($coupon) || ! $cache) {
        $coupon = M('Coupon')->where('status = 1')->order('coupon_id DESC')->getField('coupon_id,coupon_id,coupon_name,coupon_value,start_time,end_time,min_amount');
        S($cacheKey, $coupon);
    }
    return $coupon;
}

/**
 * 获取商品
 * @param  boolean true 默认缓存 ， false 数据库
 * @return array 商品数组
 */
function getGoodsList($cache = true)
{
    $cacheKey = C('CACHE_KEY_GOODS_LIST');
    $goods = S($cacheKey);
    if (empty($goods) || ! $cache) {
        $goods = M('Goods')->where('status = 1')->order('goods_id DESC')->getField('goods_id,goods_id,goods_name,store_id,goods_image');
        S($cacheKey, $goods);
    }
    return $goods;
}

/**
 * 获取区域
 * @param  boolean true 默认缓存 ， false 数据库
 * @return array 地区数组
 */
function getArea($cache = true)
{
    $cacheKey = C('CACHE_KEY_AREA');
    $area = S($cacheKey);
    if (empty($area) || ! $cache) {
        $area = M('Area')->where('status = 1')->order('sort DESC, area_id ASC')->getField('area_id,area_name');
        S($cacheKey, $area);
    }
    return $area;
}

/**
 * 获取地区
 * @param  boolean true 默认缓存 ， false 数据库
 * @return array 地区数组
 */
function getRegion($cache = true)
{
    $cacheKey = C('CACHE_KEY_REGION');
    $region = S($cacheKey);
    if (empty($region) || ! $cache) {
        $region = M('Region')->where('status = 1')->order('click DESC, region_id DESC')->getField('region_id,region_name');
        S($cacheKey, $region);
    }
    return $region;
}

/**
 * module节点
 *
 * @param $cache 是否读取缓存
 * @return array module
 */
function getModule($cache = true)
{
    $cacheKey = C('CACHE_KEY_ADMIN_MODULE');
    $modules = S($cacheKey);
    if (empty($modules) || ! $cache) {
        $modules = M('AdminModule')->where(array( 'status' => 1 ))->order('sort DESC')->getField('module_id,module_title,module_name,parent_id,module_action,hide,console');
        S($cacheKey, $modules);
    }
    return $modules;
}

/**
 * admin_group
 *
 * @param $cache 是否读取缓存
 * @return array group
 */
function getAdminGroup($cache = true)
{
    $cacheKey = C('CACHE_KEY_ADMIN_GROUP');
    $groups = S($cacheKey);
    if (empty($groups) || ! $cache) {
        $groups = M('AdminGroup')->where(array( 'status' => 1 ))->getField('group_id,group_name');
        S($cacheKey, $groups);
    }
    return $groups;
}


/**
 * 商户列表
 *
 * @param $cache 是否读取缓存
 * @return array
 */
function getStoreList($cache = true)
{
    $cacheKey = C('CACHE_KEY_STORE_LIST');
    $store = S($cacheKey);
    if (empty($store) || ! $cache) {
        $store = M('Store')->where(array('status' => array( 'gt' , 0 )))->getField('store_id,store_name');
        S($cacheKey, $store);
    }
    return $store;
}

/**
 * STORE_XX
 *
 * @param $cache 是否读取缓存
 * @return array
 */
function getStore($storeID, $cache = true)
{
    if (empty($storeID)){
        return false;
    }
    $cacheKey = C('CACHE_KEY_STORE') . $storeID;
    $store = S($cacheKey);
    if (empty($store) || ! $cache) {
        $store = M('Store')->where(array( 'store_id' => $storeID ))->find();
        S($cacheKey, $store);
    }
    return $store;
}

function getChannel($cache = true)
{
    $cacheKey = C('CACHE_KEY_CHANNEL');
    $channel = S($cacheKey);
    if (empty($channel) || ! $cache) {
        $channel = M('Channel')->where(array('status' => 1))->getField('channel_value,channel_name');
        S($cacheKey, $channel);
    }
    return $channel;
}

/**
 * member_XX
 *
 * @param $openID openid
 * @return array
 */
function getMember($openID, $cache = true)
{
    if (empty($openID)){
        return false;
    }
    $cacheKey = C('CACHE_KEY_MEMBER') . $openID;
    $member = S($cacheKey);
    if (empty($member) || ! $cache) {
        $member = M('Member')->where(array('openid' => $openID))->find();
        S($cacheKey, $member);
    }
    return $member;
}


/**
 * @param bool $cache
 * @return mixed
 */
function getAccessToken($cache = true)
{
    $cacheKey = C('CACHE_KEY_WECHAT_ACCESSTOKEN');
    $token = S($cacheKey);
    if (empty($token) || ! $cache) {
        $auth = new Com\WechatAuth(C('WECHAT_APPID'), C('WECHAT_APPSECRET'));
        $token = $auth->getAccessToken();
        $token = $token['access_token'];
        S($cacheKey, $token);
    }
    return $token;
}

/**
 * goods_XX
 *
 * @param $cache 是否读取缓存
 * @return array
 */
function getGoods($goodsID, $cache = true)
{
    if (empty($goodsID)){
        return false;
    }
    $cacheKey = C('CACHE_KEY_GOODS') . $goodsID;
    $goods = S($cacheKey);
    if (empty($goods) || ! $cache) {
        $goods = M('Goods')->where(array( 'goods_id' => $goodsID ))->find();
        S($cacheKey, $goods);
    }
    return $goods;
}

/**
 * Menu_XX
 *
 * @param $cache 是否读取缓存
 * @return array
 */
function getMenu($menuID, $cache = true)
{
    if (empty($menuID)){
        return false;
    }
    $cacheKey = C('CACHE_KEY_MENU') . $menuID;
    $menu = S($cacheKey);
    if (empty($menu) || ! $cache) {
        $menu = M('Menu')->where(array( 'menu_id' => $menuID ))->find();
        S($cacheKey, $menu);
    }
    return $menu;
}

function getMenuGoods($menuID, $goodsID, $cache = true)
{
    if (empty($menuID) || empty($goodsID)) {
        return false;
    }
    $cacheKey = C('CACHE_KEY_MENU_GOODS') . $menuID . '_' . $goodsID;
    $menu = S($cacheKey);
    if (empty($menu) || !$cache) {
        $menu = M('MenuGoods')->where(array('menu_id' => $menuID, 'goods_id' => $goodsID))->find();
        S($cacheKey, $menu);
    }
    return $menu;
}

/**
 * user_group
 *
 * @param $cache 是否读取缓存
 * @return array group
 */
function getUserGroup($storeID, $cache = true)
{
    if (empty($storeID)){
        return false;
    }
    $cacheKey = C('CACHE_KEY_USER_GROUP') . $storeID;
    $groups = S($cacheKey);
    if (empty($groups) || ! $cache) {
        $groups = M('UserGroup')->where(array( 'status' => 1, 'store_id' => $storeID ))->getField('group_id,group_name');
        S($cacheKey, $groups);
    }
    return $groups;
}


/**
 * module节点
 *
 * @param $cache 是否读取缓存
 * @return array module
 */
function getStoreModule($cache = true)
{
    $cacheKey = C('CACHE_KEY_STORE_MODULE');
    $modules = S($cacheKey);
    if (empty($modules) || ! $cache) {
        $modules = M('UserModule')->where(array( 'status' => 1 ))->order('sort DESC')->getField('module_id,module_title,module_name,parent_id,module_action,hide,class');
        S($cacheKey, $modules);
    }
    return $modules;
}


/**
 * 菜品分类
 *
 * @param $cache 是否读取缓存
 * @return array
 */
function getGoodsCategory($cache = true)
{
    $cacheKey = C('CACHE_KEY_GOODS_CATEGORY');
    $store = S($cacheKey);
    if (empty($store) || ! $cache) {
        $store = M('GoodsCategory')->where(array('status' => 1))->getField('cate_id,cate_title');
        S($cacheKey, $store);
    }
    return $store;
}